<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>品牌列表</title>
    <script src="../../lib/vue-dev/dist/vue.min.js"></script>
    <link rel="stylesheet" href="../../lib/bootstrap-3.3.7-dist/css/bootstrap.min.css">
</head>
<body>
    <div id="app">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">
                <label v-fontsize="'25px'">
                    Id:<input type="text" class="form-control" v-model="id">
                </label>
                <label v-fontweight="10">
                    Name:<input type="text" class="form-control" v-model="name" @keyup.enter="add">
                </label>
                <input type="button" value="添加" class="btn btn-primary" @click="add">
                <label>
                    搜索名称关键字:<input type="text" class="form-control" v-model="keywords" id="search" v-focus v-fontcolor="'red'">
                </label>
            </div>
        </div>

        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in search(keywords)" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime | dateFormat}}</td>
                    <td><a href="" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        Vue.config.devtools = true
        Vue.filter('dateFormat', function(date, pattern="") {
            var dt = new Date(date);
            var y = dt.getFullYear();
            var m = dt.getMonth() + 1;
            var d = dt.getDate();

            if(pattern.toLowerCase() === 'yyyy-mm-dd') {
                return `${y}-${m}-${d}`;//y + '-' + m + '-' + d;
            } else {
                var hh = dt.getHours();
                var mm = dt.getMinutes();
                var ss = dt.getSeconds();

                return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
            }
        });
        Vue.config.keyCodes.f2=113;
        Vue.directive('focus', {
            inserted: function(el) {
                el.focus();
            }
        });
        Vue.directive('fontcolor', {
            bind: function (el, binding) {
                el.style.color = binding.value;
            }
        });
        var vm = new Vue({
            el: '#app',
            data: {
                id: '',
                name: '',
                keywords: '',
                list: [
                    {id:1, name:'奔驰', ctime: new Date()},
                    {id:2, name:'宝马', ctime: new Date()}
                ]
            },
            methods: {
                add() {
                    //alert('ok');
                    var car = {id: this.id, name: this.name, ctime: new Date()};
                    this.list.push(car);
                    this.id = this.name = '';
                },
                del(id) {
                    // this.list.some((item, i) => {
                    //     if(item.id == id) {
                    //         this.list.splice(i, 1);
                    //         return true;
                    //     }
                    // });
                    var index = this.list.findIndex(item => {
                        if(item.id == id) {
                            return true;
                        }
                    });
                    this.list.splice(index, 1);
                },
                search(keywords) {
                    // var result = [];
                    // this.list.forEach(item => {
                    //     if(item.name.indexOf(keywords) != -1) {
                    //         result.push(item);
                    //     }
                    // });
                    // return result;

                    return this.list.filter(item => {
                        //if(item.name.indexOf(keywords) != -1)
                        if(item.name.includes(keywords)) {
                            return item; 
                        }
                    });
                }
            },
            filters: {
                dateFormat:function(date, pattern="") {
                    var dt = new Date(date);
                    var y = dt.getFullYear();
                    var m = dt.getMonth() + 1;
                    var d = dt.getDate();

                    if(pattern.toLowerCase() === 'yyyy-mm-dd') {
                        return `${y}-${m}-${d}`;//y + '-' + m + '-' + d;
                    } else {
                        var hh = dt.getHours().toString().padStart(2, '0');
                        var mm = dt.getMinutes().toString().padStart(2, '0');
                        var ss = dt.getSeconds().toString().padStart(2, '0');

                        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
                    }       
                }
            },
            directives: {
                'fontweight': {
                    bind: function(el, binding){
                        el.style.fontWeight = binding.value;
                    }
                },
                'fontsize': function(el, binding){
                    el.style.fontSize = parseInt(binding.value) + 'px';
                }
            }
        });
    </script>
</body>
</html>